using System;
using System.Collections;

namespace CSharp_1209.Hospital
{
    public class MedicineList
    {
        public SortedList objMedicineDetails = new SortedList();

        public void AcceptDetails()
        {
            try
            {
                char choice = 'Y';
                do
                {
                    Console.Write("Enter the medicine name : ");
                    string name = Console.ReadLine();

                    Console.Write("Enter the price : ");
                    double price = Convert.ToDouble(Console.ReadLine());

                    objMedicineDetails.Add(name, price);
                    Console.Write("Do you want to add more names? [Y/N] : ");
                    choice = Convert.ToChar(Console.ReadLine());
                } while (choice == 'Y' || choice == 'y');
            }
            catch (Exception objEx)
            {
                Console.WriteLine("Error : {0}", objEx.Message);
            }
        }

        public void DisplayDetails()
        {
            Console.WriteLine("Details of Medicines :");
            Console.WriteLine("Medicine Name \t Price");
            for (int i = 0; i < objMedicineDetails.Count; i++)
            {
                // Displaying the details of medicine using GetKey() and GetByIndex() methods.
                Console.WriteLine("{0}\t\t {1:C2}", objMedicineDetails.GetKey(i), objMedicineDetails.GetByIndex(i));
            }
            //Count property of the SortedList class is used to display total count
            Console.WriteLine("Total number of medicines : " + objMedicineDetails.Count);
        }

        public bool Modify()
        {
            double price;
            Console.Write("Enter the name of the medicine to change its price : ");
            string name = Console.ReadLine();
            // Contains() method of SortedList class is used to check for medicine name in the list
            if (objMedicineDetails.Contains(name))
            {
                Console.Write("Enter new price : ");
                price = Convert.ToDouble(Console.ReadLine());
                // IndexOfKey() method is used to retrieve the index of the specified medicine name from the list 
                int index = objMedicineDetails.IndexOfKey(name);

                // SetByIndex() method is used to modify the price of Medicine at the specific index
                objMedicineDetails.SetByIndex(index, price);
                return true;

            }
            else
                return false;
        }

        public bool Remove()
        {
            string choice;
            Console.Write("Enter the name of the medicine : ");
            choice = Console.ReadLine();
            // Contains() method is used to check for the name of medicine in the list
            if (objMedicineDetails.Contains(choice))
            {
                // Remove() method is used to remove details of the medicine from the list
                objMedicineDetails.Remove(choice);
                return true;
            }
            else
                return false;
        }

        public void Search()
        {
            string choice;
            Console.Write("Enter the name of the medicine : ");
            choice = Console.ReadLine();
            if (choice != "")
            {
                // ContainsKey() method is used to check for the name of medicine in the list
                if (objMedicineDetails.ContainsKey(choice))
                {
                    Console.WriteLine("Record Found!");
                    Console.WriteLine("Medicine Name : {0}", choice);
                    // Displaying price of the medicine using the Item property
                    Console.WriteLine("Price : {0:C2}", objMedicineDetails[choice]);
                    objMedicineDetails[choice] = "";
                }
                else
                    Console.WriteLine("Record Not Found!");
            }
            else
            {
                Console.WriteLine("Invalid Input!");
            }
        }
    }
}
